% Penalized1
% Penalized1 has global optimal at 0 in range [-50, 50]
function result = Penalized1(x)
total_a = 0;
len = length(x);
for i = 1:len - 1
    temp = (y_ith(x(i)) - 1)^2 * (1 + 10*sin(pi*y_ith(x(i+1)))^2);
    total_a = total_a + temp;
end
total_b = 0;
for i = 1:len
    total_b = total_b + u(x(i), 10, 100, 4);
end
result = (pi / len) * (10*sin(pi*y_ith(x(1)))^2 + total_a + ...
                    (y_ith(x(len)) -1)^2) + total_b;
end

function result = y_ith(x_ith)
result = 1 + (1/4) * (x_ith + 1);
end

function result = u(x_ith, a, k ,m)
if x_ith > a
    u = k * (x_ith - a)^m;
elseif x_ith >= -a
    u = 0;
else
    u = k * (-x_ith - a)^m;
end
result = u;
end